package com.demo.pong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 *  @author Jofo
 */
@Configuration
@EnableAsync
public class TaskExecutorConfig {
    @Bean("taskExecutor")
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //Number of core threads
        executor.setCorePoolSize(10);
        //Maximum number of threads
        executor.setMaxPoolSize(20);
        //The length of the queue
        executor.setQueueCapacity(8);
        //The thread pool maintains the idle time allowed by threads
        executor.setKeepAliveSeconds(60);
        //A thread is a policy for handling a rejected task, that is, when no threads are available
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //The prefix of the task executor, which is output when the log is printed
        executor.setThreadNamePrefix("task-thread-");
        return executor;
    }
}

